/*
 * Wantmore - Service Delivery Platform.
 *
 * $Id: PhotoDaoImpl.java 53 2010-10-11 12:13:05Z alexey.batyuta@gmail.com $
 *
 * Copyright 2010 by Sunbay Software Crimea Ltd.
 * All rights reserved
 */
package ua.crimea.bat.ss.dao;


import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import ua.crimea.bat.ss.model.beans.Album;
import ua.crimea.bat.ss.model.beans.Image;

import java.util.ArrayList;
import java.util.Collection;

/**
 * User: bat
 * Date: Mar 30, 2010
 * Time: 1:02:31 PM
 */
@Repository
@Transactional
public class PhotoDaoImpl extends HibernateDaoSupport implements PhotoDao {

    @Transactional(readOnly = true)
    @SuppressWarnings("unchecked")
    //@Secured({"ROLE_ADMIN"})
    public Collection<Album> findPrivateAlbums() throws DataAccessException {
        Collection<Album> albums = getHibernateTemplate().find("from Album album");

        if (null == albums) {
            albums = new ArrayList<Album>();
        }
        return albums;
    }

    @Transactional(readOnly = true)
    @SuppressWarnings("unchecked")
    public Collection<Album> findPublicAlbums() throws DataAccessException {
        Collection<Album> albums = getHibernateTemplate().find("from Album album where album.isPublic = TRUE");

        if (null == albums) {
            albums = new ArrayList<Album>();
        }
        return albums;
    }

    @Transactional(readOnly = true)
    public Album loadAlbum(Integer id) throws DataAccessException {
        return (Album) getHibernateTemplate().get(Album.class, id);
    }

    //@Secured({"ROLE_ADMIN"})

    public void storeAlbum(Album album) throws DataAccessException {
        save(album);
    }

    protected void save(Object object) {
        getHibernateTemplate().saveOrUpdate(object);
        /*Transaction transaction = null;
        try {
            transaction = getHibernateTemplate().getSessionFactory().getCurrentSession().beginTransaction();
            transaction.begin();
            getHibernateTemplate().saveOrUpdate(object);
        } finally {
            if (transaction != null && transaction.isActive() && !transaction.wasCommitted()) {
               transaction.commit();
            }
        }*/
    }

    //@Secured({"ROLE_ADMIN"})

    public void deleteAlbum(Integer id) throws DataAccessException {
        Album album = loadAlbum(id);
        getHibernateTemplate().delete(album);
    }

    @Transactional(readOnly = true)
    public Image loadImage(Integer id) throws DataAccessException {
        return (Image) getHibernateTemplate().get(Image.class, id);
    }

    //@Secured({"ROLE_ADMIN"})

    public void storeImage(Image image) throws DataAccessException {
        save(image);
    }

    //@Secured({"ROLE_ADMIN"})

    public void deleteImage(Integer id) throws DataAccessException {
        Image image = loadImage(id);
        getHibernateTemplate().delete(image);
    }

}
